<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Db;

class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {
        $ids = $this->request->param('ids');
        if (empty($ids)) {
            $data = Db::name('kmh')->select(); 
        } else{
            $data = Db::name('kmh')->where('id','in',$ids)->select(); 
        }
        
 
        $blockSize = 36;
        $rowSize = 6;

       
        $organizedData = [];
        for ($i = 0; $i < ceil(count($data) / $blockSize); $i++) {
            $startIndex = $i * $blockSize;
            $endIndex = min(($i + 1) * $blockSize, count($data));
            $block = array_slice($data, $startIndex, $endIndex - $startIndex);

            $rows = [];
            for ($j = 0; $j < ceil(count($block) / $rowSize); $j++) {
                $rowStartIndex = $j * $rowSize;
                $rowEndIndex = min(($j + 1) * $rowSize, count($block));
                $rows[] = array_slice($block, $rowStartIndex, $rowEndIndex - $rowStartIndex);
            }

            $organizedData[] = $rows;
        }

        $this->view->assign('organizedData', $organizedData);
        return $this->view->fetch();
    }
}
